"""初始化窗口"""
import pygame
from source.gui.settints import Settings
from source.gui.ship import Ship
import source.gui.game_funtions as gf
from pygame.sprite import Group
from source.gui.button import Buttion
from source.gui.game_stats import Game_stats
from source.gui.music import Music
from source.gui.scoreboard import Scoreboard


def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    gui_settings = Settings()
    screen = pygame.display.set_mode((gui_settings.screen_width, gui_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    # 创建一艘飞船
    gui_ship = Ship(screen, gui_settings)
    # 创建一个用于储存子弹的编组
    bullets = Group()

    # 创建外星人编组
    aliens = Group()
    # 创建外星人群
    gf.create_fleet(gui_settings, screen, gui_ship, aliens)

    # 游戏状态
    state = Game_stats(gui_settings)
    # 得分
    score = Scoreboard(gui_settings, screen, state)
    # 创建play按钮
    play_button = Buttion(gui_settings, screen, "Play")

    # 加载背景音乐
    music = Music()

    # 开始游戏的主循环
    while True:
        # 监听键盘和鼠标事件
        gf.check_events(gui_settings, state, score,play_button, screen, gui_ship, aliens, bullets, music)
        if state.game_active:
            gui_ship.updateMoving()
            # 更新子弹的位置，并删除消失的子弹
            gf.update_bullets(gui_settings, screen, state, score, gui_ship, aliens, bullets,music)
            # 更新外星人位置
            gf.update_aliens(gui_settings,screen,state,score,gui_ship, aliens,bullets)
        else:
            music.close_music()
        # 显示屏幕,设置背景颜色
        gf.update_screen(gui_settings, screen, state, score, gui_ship, aliens, bullets, play_button)


run_game()
